/**=========================================================
 * Directive: simple-select.js
 =========================================================*/
App.directive('simpleSelect', ['Utils',
    function(utils) {
        'use strict';
        
        return {
            restrict: 'EA',
            replace: true,
            template: function($element,$attrs){
                var options = $attrs.optionModel,
                    ngModel = utils.isEmpty($attrs.ngModel) ? 'selects.' + options + '.selected' : $attrs.ngModel,
                    name = utils.isEmpty($attrs.name) ? '' : ('name="' + $attrs.name + '"'),
                    required = $attrs.required == undefined ? '' : ' required ',
                    minimumInputLength = $attrs.minimumInputLength == undefined ? '3' : $attrs.minimumInputLength,
                    refresh = $attrs.refresh == undefined ? '' : ' refresh="' + options + 'Query($select.search)" minimum-input-length="'+ minimumInputLength + '" refresh-delay=300 ',
                    multiple = $attrs.multiple == undefined ? '' : ($attrs.refresh == undefined ? ' multiple close-on-select="true"' : ' multiple'),
                    selectedValue = $attrs.multiple == undefined ? '{{$select.selected.name}}' : '{{$item.name}}',
                    filter = $attrs.filter == undefined ? ' | filter: $select.search' : $attrs.filter,
                    highlight = $attrs.filter == undefined ? ' | highlight: $select.search' : '',
                    animated = $attrs.multiple == undefined ? 'fadeIn' : 'fadeIn',
                    allowClear = $attrs.allowClear == 'true' ? 'allow-clear="true"' : '',
                    placeholder = utils.isEmpty($attrs.placeholder) ? '' : 'placeholder="' + $attrs.placeholder + '"',
                    divClass = ($attrs.multiple != undefined && $attrs.refresh == undefined) ? 'class="input-group"' : '',
                    selectAllButton = ($attrs.multiple != undefined && $attrs.refresh == undefined) ? '\
                        <span class="input-group-btn">\
                            <button title="选择全部" type="button" ng-click="' + options + 'AllSelected()" class="btn btn-default">\
                                <small>\
                                    <em class="fa fa-check text-muted icon-lg"></em>\
                                </small>\
                            </button>\
                        </span>' : '';
                
                return '\
                    <div ' + divClass + '>\
                        <ui-select reset-search-input="true" ng-model="' + ngModel + '" ' + name + '' + required + multiple + '>\
                            <ui-select-match ' + allowClear + placeholder + '>' + selectedValue + '</ui-select-match>\
                            <ui-select-choices class="animated ' + animated + '"' + refresh + ' repeat="option.value as option in selects.' + options + '.datas ' + filter + '">\
                                <span ng-bind-html="option.name ' + highlight + '"></span>\
                            </ui-select-choices>\
                        </ui-select>' + selectAllButton + '\
                    </div>\
                '
            },
            link: function($scope, $element, $attrs){
                var options = $attrs.optionModel,
                    ngModel = utils.isEmpty($attrs.ngModel) ? 'selects.' + options + '.selected' : $attrs.ngModel;
                if ($attrs.multiple != undefined && $attrs.refresh == undefined){
                    $scope[options + 'AllSelected'] = function(){
                        var datas = [],
                            allDatas = $scope.selects[$attrs.optionModel].datas;
                        for (var i = 0; i < allDatas.length; i++){
                            if (allDatas[i].value != '-1'){
                                datas.push(allDatas[i].value);
                            }
                            utils.setChildValByStr($scope,ngModel,datas);
                        }
                    }
                }
            }
        }
    }
]);
